<?php

// options
        $EMAIL = 'you@yoursite.com';
        $PASSWORD = 'yourpassword';
        $cookie_file_path = "cc.cc";
        $LOGINURL = "https://svpr-isg-a1.isaca.org/ISGweb/LogIn/Login.aspx";
        $agent = "Nokia-Communicator-WWW-Browser/2.0 (Geos 3.0 Nokia-9000i)";


        // begin script
        $ch = curl_init();

        // extra headers
        $headers[] = "Accept: */*";
        $headers[] = "Connection: Keep-Alive";

        // basic curl options for all requests
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//        curl_setopt($ch, CURLOPT_USERAGENT, $agent);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);

        // set first URL
        curl_setopt($ch, CURLOPT_URL, $LOGINURL);

        // execute session to get cookies and required form inputs
        $content = curl_exec($ch);
//echo $content;die();
        // grab the hidden inputs from the form required to login
        $fields = Array
(
    '__EVENTTARGET' => 'btnGo',
    '__EVENTARGUMENT' => '',
    '__VIEWSTATE' => '/wEPDwUKMTcxMDAyNTEzOA9kFgQCAQ9kFgICAQ8WAh4EaHJlZgUiL0lTR3dlYi9TdHlsZXMvaXdlYkxvZ2luU3R5bGVzLmNzc2QCAw8WBB4FY2xhc3MFCGlzZ19ib2R5HgpvbnBhZ2VoaWRlBRFyZUVuYWJsZUJ1dHRvbigpOxYCAgIPZBYCAgEPZBYCZg9kFgJmD2QWAmYPZBYCZg9kFgJmD2QWAmYPZBYCZg9kFgJmD2QWAmYPZBYCZg9kFgJmD2QWCGYPZBYCZg9kFgRmDw8WAh4EVGV4dGRkZAIBDw8WAh8DZGRkAgIPZBYCZg9kFgJmDw8WAh8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RW1haWxTUGUeE0Rpc2FibGVPblByb2Nlc3NpbmdnHhBHZW5lcmF0ZVBhZ2VEYXRhaB8DBQVMb2dpbh4EXyFTQgICHg9DdXN0b21FbWFpbEZpbGVlHghDc3NDbGFzcwUKaXNnX2J1dHRvbh4JQ3VzdG9tVVJMZR4HVG9vbFRpcGQeC0N1c3RvbVVSTFNQZR4MUHJldmFsaWRhdG9yZR4NUG9zdHZhbGlkYXRvcmUWAh4HT25DbGljawXSCCAgaWYgKHR5cGVvZihQYWdlX0NsaWVudFZhbGlkYXRlKSE9J3VuZGVmaW5lZCcpIA0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHsNCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHR5cGVvZihQYWdlX0NsaWVudFZhbGlkYXRlKSA9PSAnZnVuY3Rpb24nKSANCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgew0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKFBhZ2VfQ2xpZW50VmFsaWRhdGUoJycpID09IGZhbHNlKSANCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHsNCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoaW5zaWRlSWZyYW1lKCkpIGRvRm9yY2VSZXNpemUoKTsNCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICByZXR1cm4gZmFsc2U7IA0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQ0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGluc2lkZUlmcmFtZSgpKSBkb0ZvcmNlUmVzaXplKCk7DQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC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c3Bhbj48L2I+HxJkZGQCBg9kFgJmD2QWAmYPDxYCHwNkZGRkehdUP2IIFjjaal9SmEmR7YmkvqQ=',
    '__EVENTVALIDATION' => '/wEWBQLxn827BAKX4e7KBAKCsvS1AwKBk/m1CwKBsdCMByULrkAebPmf6BR5oj1ODO0srM4z',
    'txttxtUsername' => 'akinlax',
    'txttxtPassword' => 'Flourish75'
);

        // get x value that is used in the login url
        $x = '';
        if (preg_match('/op\.asp\?x=(\d+)/i', $content, $match)) {
            $x = $match[1];
        }

        //$LOGINURL   = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn";
        $LOGINURL = "https://svpr-isg-a1.isaca.org/ISGweb/LogIn/Login.aspx";

        // set postfields using what we extracted from the form
        $POSTFIELDS = http_build_query($fields);

        // change URL to login URL
        curl_setopt($ch, CURLOPT_URL, $LOGINURL);

        // set post options
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);

        // perform login
        $result = curl_exec($ch);

        
        
//$headers[] = "Accept: */*";
//        $headers[] = "Connection: Keep-Alive";

        // basic curl options for all requests
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        //curl_setopt($ch, CURLOPT_USERAGENT, $agent);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);

        // set first URL
        curl_setopt($ch, CURLOPT_URL, 'http://www.isaca.org/myisaca/pages/default.aspx');

        // execute session to get cookies and required form inputs
        print_r(curl_exec($ch));
        
        
        
        function getFormFields($data) {
            if (preg_match('/(<form action="op.*?<\/form>)/is', $data, $matches)) {
                $inputs = getInputs($matches[1]);

                return $inputs;
            } else {
                die('didnt find login form');
            }
        }

        function getInputs($form) {
            $inputs = array();

            $elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);

            if ($elements > 0) {
                for ($i = 0; $i < $elements; $i++) {
                    $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);

                    if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
                        $name = $name[1];
                        $value = '';

                        if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {
                            $value = $value[1];
                        }

                        $inputs[$name] = $value;
                    }
                }
            }

            return $inputs;
        }